<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class VillageThingsComment extends Model
{

    use SoftDeletes;
    protected $table = 'village_things_comments';

    /**
     * 定义主键
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * 禁止批量赋值的字段
     *
     * @var array
     */
    protected $guarded = ['id','created_at','deleted_at'];

    /**
     * 软删除应该被调整为日期的属性
     * [$dates description]
     * @var [type]
     */
    protected $dates=[
        'created_at', 'updated_at','deleted_at'
    ];
    /*追加字段*/
    protected $appends = ['imgs_url','video_url','zaned','followed','reply_user_name'];

    /**
     * 关联回复用户
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function replyer()
    {
        return $this -> hasOne(User::class,'id','reply_user_id');
    }

    //给图片添加链接
    public function getImgsUrlAttribute()
    {
        $newarr = [];
        if($this -> attributes['imgs']){
            $arr = json_decode($this -> attributes['imgs']);
            foreach($arr as $v){
                $newarr[] = config('app.img_url') . $v;
            }
        }
        return $newarr;
    }

    public function getVideoUrlAttribute()
    {
        if($this -> attributes['video']){
            return config('app.img_url') . $this -> attributes['video'];
        }
        return '';
    }


    public function commenter()
    {
        return $this -> hasOne(User::class,'id','user_id');
    }


    // 点赞
    public function getZanedAttribute()
    {
        if(!$user_id = auth() -> id()){
            return false;
        }
        $where = [
            'village_things_id' => $this -> village_things_id,
            'operate_type'   => 1,
            'user_id'        => $user_id,
            'obj_id'         => $this -> id,
        ];

        return VillageThingsOperateLogs::where($where) -> first()
            ? true : false;

    }


    public function getFollowedAttribute()
    {
        if(!$user_id = auth() -> id()){
            return false;
        }

        return UserFollow::where('user_id',$user_id) -> where('follow_user_id',$this -> user_id) -> first()
            ? true : false;
    }

    // 回复用户的id
    public function getReplyUserNameAttribute()
    {
        $user = User::find($this -> reply_user_id);
        return $this -> reply_user_id ? ($user ? ($user -> real_name ?: $user -> user_name) : null) : null;
    }

    /*
     * 存储预约评论内容
     * @return array
     */
    public static function store($param){

        if($param['imgs']){
            $arr = [];
            foreach(explode(',',$param['imgs']) as $v){
                $arr[] = $v;
            }
            $param['imgs'] = json_encode($arr);
        }

        if($param['reply_id']){

            $param['reply_user_id'] = self::where('id',$param['reply_id']) -> value('user_id') ;
        }else{
            $param['reply_id'] = 0 ;
        }


        $param['user_id']  = auth() -> id();


        $res = self::create($param);
        return $res;
    }


    /*
     * 判断是否在评论自己
     * @return
     */
    public static function check($village_things_id, $reply_id = 0)
    {
        $user_id = auth() -> id();

        if($reply_id){
            $comment = self::find($reply_id);
            if($comment -> user_id == $user_id){
                return true;
            }
        }else{
            $villageThings = VillageThings::find($village_things_id);
            if($villageThings -> user_id == $user_id){
                return true;
            }
        }

        return false;

    }
}
